<?php

# 'menu_item' table management
class MenuItem extends Model
{
	var $id = null;
	var $menu_id = null;
	var $name = '';
	var $url = '';
	var $content_id;
	var $menu_item_id;
	var $position;
	var $created_at;
	var $updated_at;
	var $active = 1;
	var $online = 1;
	
	function MenuItem()
	{
		parent::Model();
		$this->created_at = date_db_format();
	}
	
	/**
	 * Retrieve useful data from DB and assign values to object's fields
	 * @return	true if an ID is provided in order to retrieve data, false otherwise.
	 */
	function get_by_id($id)
	{
		$this->db->select('id, url, content_id, name, online, position, menu_id, menu_item_id, created_at, updated_at')
		         ->from('menu_item')
		         ->where('id', $id)
		         ->where('active', 1);
				
		$query = $this->db->get();
		return $query->row();
	}
	
	/**
	 * Retrieve useful data from DB for all menus that are in use
	 * @return	retrieved datas
	 */
	function get_by_menu($menu_key)
	{
		$sql = 'select m.id, m.name, m.url, m.content_id, m.position,
		               c.title as content_title,
		               (parent.name is null) as is_first_level,
		               ifnull(parent.position, m.position) as parent_position,
		               if(parent.position is null, 0, m.position) as self_position,
					   ifnull(parent.online, 1) as parent_online
		        from menu_item m
		        join menu mm on mm.id = m.menu_id
		        left join menu_item parent on m.menu_item_id = parent.id
		        left join content c on c.id = m.content_id
		        where mm.key = "'.$menu_key.'"
		              and m.online = 1 
		              and m.active = 1
		        having parent_online = 1
		        order by parent_position, self_position;';
				
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	function get_by_menu_id($menu_id)
	{
		$sql = 'select m.id, m.name, m.url, m.content_id, m.position, m.online,
		               c.title as content_title,
		               (parent.name is null) as is_first_level,
		               ifnull(parent.position, m.position) as parent_position,
		               if(parent.position is null, 0, m.position) as self_position,
					   ifnull(parent.online, 1) as parent_online
		        from menu_item m
		        left join menu_item parent on m.menu_item_id = parent.id
		        left join content c on c.id = m.content_id
		        where m.menu_id = '.intval($menu_id).'
		              and m.active = 1
		        order by parent_position, self_position;';
				
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	function save($item)
	{
		$item->updated_at = date_db_format();
		if (!$item->position) {
			$item->position = $this->get_next_position($item->menu_item_id);
		}
		
		if ($item->id == null) {
			return $this->db->insert('menu_item', $item);
		}
		else {
			$this->db->where('id', $item->id);
			return $this->db->update('menu_item', $item); 
		}
	}
	
	function get_next_position($first_level_id = null)
	{
		$this->db->select('max(position) as last_position')
			     ->from('menu_item')
			     ->where('active', 1)
			     ->where('menu_item_id', $first_level_id ? $first_level_id : null);
		$result = $this->db->get()->result();
		return intval($result[0]->last_position) + 1;
	}
	
	function delete($id)
	{
		$id = intval($id);
		// on supprime également les éléments enfants
		$sql = 'UPDATE menu_item
		        SET active = 0
		        WHERE id = '.$id.' OR menu_item_id = '.$id.';';
		return $this->db->query($sql);
	}
	
	/**
	 * Construit le lien correspond à un menu_item
	 * (un menu_item peut pointer sur une page de contenu ou sur un lien dur)
	 * @return anchor tag
	 */
	function build_anchor($row)
	{
		$link = $this->get_link($row);
		return $link ? anchor($link, $row->name) : '<span class="no-link">'.$row->name.'</span>';
	}
	
	function get_link($item)
	{
		// lien dur
		if ($item->url) {
			return $item->url;
		}
		// lien vers un content
		if ($item->content_id) {
			// on ajoute le titre du content à la fin pour rendre l'url facilement googlable
			// mais seul 'content_id' sera exploité par le contrôleur
			return 'contents/show/'.$item->content_id.'-'.url_title($item->content_title);
		}
		// bloc de sous menu
		return '';
	}
	
	function is_content_link($item)
	{
		return $item->content_id;
	}
	
	function is_intern_link($item)
	{
		return $item->url && !$this->is_extern_link($item);
	}
	
	function is_extern_link($item)
	{
		return preg_match('!^\w+://! i', $item->url);
	}
	
	function is_link($item)
	{
		return $item->content_id || $item->url;
	}
	
	function has_no_link($item)
	{
		return !$item->content_id && !$item->url;
	}
}

